package com.lzw.controller;

import com.alibaba.fastjson.JSONObject;
import com.lzw.bean.User;
import com.lzw.bean.result.Meta;
import com.lzw.bean.result.ResultObject;
import com.lzw.bean.result.UsersResult;
import com.lzw.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @auther: lzw
 * @date: 2021/10/7 - 10 - 07 - 20:52
 * @Description: com.lzw.controller
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("/user/userList/{pageNum}/{pageSize}")
    public JSONObject getUserList(@PathVariable("pageNum") Integer pageNum
            ,@PathVariable("pageSize") Integer pageSize
            ,@RequestParam("query") String query){
        log.info("参数分解："+query+"-"+pageNum+"-"+pageSize);
        UsersResult usersResult = usersService.getUsersList(pageNum,pageSize,query);
        System.out.println("service层的userlist结果：！！！！！！！！！！"+JSONObject.toJSONString(usersResult));
        ResultObject resultObject = new ResultObject();
        resultObject.setData(usersResult);
        resultObject.setMeta(new Meta("获取成功",200));
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));

    }

    @RequestMapping(value = "/user/state/{id}/{mg_state}",method = RequestMethod.PUT)
    public void updateUserState(@PathVariable("id") Integer id
            ,@PathVariable("mg_state") boolean mg_state){
        log.info("id===="+id);
        log.info("mg_state======"+mg_state);
        Integer res = usersService.updateUserStatus(id,mg_state);
        if (res==0){
            log.info("未修改成功");
        }
    }

    @RequestMapping(value = "/user/adduser", method = RequestMethod.POST)
    public JSONObject addUser(@RequestBody User user){
//        String username = (String) params.get("username");
//        String password = (String) params.get("password");
//        String email = (String) params.get("email");
//        String mobile = (String) params.get("mobile");
        log.info("username="+user.getUsername()+"#######password="+user.getPassword());
        Integer res = usersService.addUser(user);
        User userinfo = null;
        if (res == 1){
            log.info("添加用户成功！！！");
            userinfo = usersService.getUser(user);
        }
        ResultObject resultObject = new ResultObject();
        resultObject.setData(userinfo);
        resultObject.setMeta(new Meta("添加成功",201));
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public JSONObject getUser(@PathVariable("id") Integer id){
        User user = usersService.getUser(id);
        log.info("需要查看用户的id是："+id);
        Meta meta = null;
        if (user != null){
            meta = new Meta("找到用户",200);
        }else {
            meta = new Meta("用户不存在",500);
        }
        ResultObject resultObject = new ResultObject();
        resultObject.setData(user);
        resultObject.setMeta(meta);
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.PUT)
    public void setUser(@PathVariable("id") Integer id, @RequestBody Map map){
        String email = (String) map.get("email");
        String mobile = (String) map.get("mobile");
        log.info("要修改的邮箱是："+email);
        log.info("要修改的手机号是："+mobile);
        log.info("要修改的用户id是："+id);
        Integer res = usersService.updateUserInfo(id,email,mobile);
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public JSONObject deleteUser(@PathVariable("id") Integer id){
        Integer res = usersService.deleteUser(id);
        Meta meta = null;
        if (res == 1){
            meta = new Meta("已删除用户",200);
        }else {
            meta = new Meta("删除用户失败",500);
        }
        ResultObject resultObject = new ResultObject();
        resultObject.setMeta(meta);
        resultObject.setData(null);
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));
    }
}




